import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useAsyncAxios } from '@/utils/asyncFuncStore'
import type { ColumnsList } from '@/types/loginType'

export const useColumnStore = defineStore('useColumnStore', () => {
  const listOriginData = ref<ColumnsList[]>([])
  const listData = ref<ColumnsList[]>([])
  const pageTotal = ref(0)

  // 获取初始值
  async function columnsData() {
    const { getColumnsData } = useAsyncAxios()
    return await getColumnsData()
  }
  // 获取指定id的专栏
  async function getIdColumn(id: string) {
    const { getColumn } = useAsyncAxios()
    return await getColumn(id)
  }

  // 页码切换显示数据
  function select(key: string) {
    // columnId, name;
    if (!key) {
      changePage(1)
    } else {
      listData.value = listOriginData.value.filter((item) => item.id === key || item.name === key)
      pageTotal.value = listData.value.length
    }
  }
  // 获取指定值的
  async function getFollowigColumns(id: string) {
    const { searchFollowigColumns } = useAsyncAxios()
    const { status, data } = await searchFollowigColumns(id)
    if (status === 200) {
      pageTotal.value = (data as ColumnsList[]).length
      return { status, data }
    } else {
      pageTotal.value = listOriginData.value.length
      return { status, data }
    }
  }

  // 赋值原始值
  function assignListOriginData(data: ColumnsList[]) {
    listOriginData.value = data
  }

  // 赋值渲染值, 翻页
  function assignListData(data: ColumnsList[]) {
    listData.value = data
  }

  function changePage(page: number) {
    if (page === 1) {
      const start = 0
      const end = 24
      listData.value = listOriginData.value.slice(start, end)
    } else {
      const start = (page - 1) * 24
      const end = page * 24
      listData.value = listOriginData.value.slice(start, end)
    }
  }

  return {
    listOriginData,
    listData,
    pageTotal,
    columnsData,
    getIdColumn,
    select,
    assignListOriginData,
    assignListData,
    changePage,
    getFollowigColumns
  }
})
